package com.samoyed.credit;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.manu.mdatepicker.MDatePicker;
import com.samoyed.credit.BaseActivity;
import com.samoyed.credit.Modle.ListDataModle;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ManualAccountActivity extends BaseActivity {

    private TextView tv_order_time;
    int mYear, mMonth, mDay;
    final int DATE_DIALOG = 1;
    private EditText amountEt;
    private EditText contentEt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_account);
        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true)
                .statusBarColor(R.color.white).init();
        tv_order_time = findViewById(R.id.tv_order_time);
        amountEt = findViewById(R.id.amount_et);
        contentEt = findViewById(R.id.et_content);
        findViewById(R.id.close_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPregressDialog("正在保存中，请稍后...");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ListDataModle listDataModle = new ListDataModle();
                        listDataModle.setType(2);
                        listDataModle.setOrdertype(2);
                        listDataModle.setAmount(amountEt.getText().toString());
                        listDataModle.setTime(tv_order_time.getText().toString());
                        listDataModle.setOrdercontent(contentEt.getText().toString());
                        Intent intent = new Intent().putExtra("mode", listDataModle);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }, 2200);
            }
        });

        tv_order_time.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dataPickter();
            }
        });

    }

    private void dataPickter() {
        MDatePicker.create(this)
                //附加设置(非必须,有默认值)
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.BOTTOM)
                .setSupportTime(false)
                .setTwelveHour(true)
                //结果回调(必须)
                .setOnDateResultListener(new MDatePicker.OnDateResultListener() {
                    @Override
                    public void onDateResult(long date) {
                        // date
                        tv_order_time.setText(longToDate(date));
                    }
                })
                .build()
                .show();
    }

    /**
     * @param lo 毫秒数
     * @return String yyyy-MM-dd HH:mm:ss
     * @Description: long类型转换成日期
     */
    public static String longToDate(long lo) {
        Date date = new Date(lo);
        String str = dateToWeek(date);
        SimpleDateFormat sd = new SimpleDateFormat("MM月dd日");
        return sd.format(date) + " " + str;
    }

    public
    static String dateToWeek(Date dateTime) {
        String[] weekDays
                = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
        };
        Calendar calendar
                =
                Calendar.getInstance();
        calendar.setTime(dateTime);

//
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if
        (w < 0) {
            w = 0;
        }

        return
                weekDays[w];
    }

}